/**     
 * @Title: DemoServlet.java   
 * @Package me.xxy.mq.demo.servlet   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年5月30日 下午5:50:46   
 * @version V1.0     
 */
package me.xxy.mq.demo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.xxy.mq.core.Message;
import me.xxy.mq.core.MessageTrunk;
import me.xxy.mq.demo.MessageType;
import me.xxy.mq.demo.SpringBeanAware;
import me.xxy.mq.demo.entity.DemoMessage;

import org.springframework.util.StringUtils;

/**
 * @ClassName: DemoServlet
 * @Description: TODO
 * @author weiwei
 * @date 2018年5月30日 下午5:50:46
 * 
 */
public class DemoServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	public DemoServlet() {

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 取HTTP参数
		String value = request.getParameter("message");
		if (StringUtils.isEmpty(value)) {
			response.getOutputStream().print("message cannot be empty.");
			return;
		}
		// 获取mt实例
		MessageTrunk mt = (MessageTrunk) SpringBeanAware.getBean("messageTrunk");
		Message message = new Message(MessageType.DEMO_MESSAGE, new DemoMessage(value, "111121333", "addsddssd"));
		ConsoleWebSocket.socket.sendMessage("开始插入消息到MT");
		// 消息入MT
		long startTime = System.currentTimeMillis();
		mt.put(message);
		ConsoleWebSocket.socket.sendMessage("结束插入消息到MT,一共耗时:" + (System.currentTimeMillis() - startTime));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
